import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import { resolve } from 'path'

export default defineConfig({
  plugins: [vue()],
  resolve: {
    alias: {
      '@': resolve(__dirname, 'src')
    }
  },
  define: {
    global: 'globalThis',
  },
  server: {
    port: 5173,
    // 解决Vue Router history模式刷新404问题
    historyApiFallback: {
      index: '/index.html',
      rewrites: [
        // 确保所有路由都回退到index.html，但排除API和静态资源
        { from: /^\/(?!api|uploads|assets|@|node_modules).*$/, to: '/index.html' }
      ],
      // 启用详细日志，便于调试
      verbose: true,
      // 禁用点文件检查，允许所有路径
      disableDotRule: true
    },
    proxy: {
      '/api': {
        target: 'http://localhost:8084',
        changeOrigin: true,
        secure: false
      },
      '/uploads': {
        target: 'http://localhost:8084',
        changeOrigin: true,
        secure: false
      },
      '/ws': {
        target: 'http://localhost:8084',
        changeOrigin: true,
        secure: false,
        ws: true
      }
    }
  },
  build: {
    // 确保构建时也处理路由问题
    rollupOptions: {
      output: {
        manualChunks: {
          vendor: ['vue', 'vue-router', 'pinia'],
          elementPlus: ['element-plus']
        }
      }
    }
  }
})